/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ButtonPanel.java
 *
 * Created on 06/03/2013, 13:58:01
 */
package gui.netbeans;

import gui.DrawPanel;
import gui.eventListener.Mouse;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Hashtable;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.ListCellRenderer;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import dados.Curva;
import dados.Ponto;

/**
 * 
 * @author dfop
 */
public class ButtonPanel extends javax.swing.JPanel {

	private DrawPanel target;
	private DefaultListModel listaDePontos;
	private Color[] colors;
    /** Creates new form ButtonPanel */
	
	public ButtonPanel() {
		initComponents();
	}
	
    public void inicializando(DrawPanel draw) {
    	this.target = draw;
    	this.setPreferredSize(new Dimension(180, 600));
        checkBoxCurva.setSelected(true);
        checkBoxPontos.setSelected(true);
        
        listaDePontos = new DefaultListModel();
        listPontos.setModel(listaDePontos);
        comboBoxCurvas.removeAllItems();
		colors = new Color[]{Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.ORANGE, Color.RED, Color.YELLOW};
       
    }

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		comboBoxCurvas = new javax.swing.JComboBox();
		labelCurva = new javax.swing.JLabel();
		labelPontos = new javax.swing.JLabel();

		sliderPolig = new javax.swing.JSlider(JSlider.HORIZONTAL,
				(int) DrawPanel.MIN_SPACING_BETWEEN_FRAMES,
				(int) DrawPanel.MAX_SPACING_BETWEEN_FRAMES,
				(int) DrawPanel.MIN_SPACING_BETWEEN_FRAMES);
		sliderPolig.setMajorTickSpacing((int) DrawPanel.SPEED_INCREMENT);
		sliderPolig.setPaintTicks(true);

		Hashtable<Integer, Component> labelTableSliderPolig = new Hashtable<Integer, Component>();
		labelTableSliderPolig.put((int) DrawPanel.MIN_SPACING_BETWEEN_FRAMES,
				new JLabel("Devagar"));
		labelTableSliderPolig.put((int) DrawPanel.MAX_SPACING_BETWEEN_FRAMES,
				new JLabel("Rapido"));
		sliderPolig.setLabelTable(labelTableSliderPolig);
		sliderPolig.setPaintLabels(true);

		sliderPolig.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent evt) {
				sliderPoligStateChanged(evt);
			}
		});

		sliderFatorT = new javax.swing.JSlider(JSlider.HORIZONTAL, 0, 1000, 500);
		
		sliderFatorT.addChangeListener(new ChangeListener() {
		
			@Override
			public void stateChanged(ChangeEvent arg0) {
				sliderFatorTStateChanged(arg0);
		
			}
		});
		
		Hashtable<Integer, Component> labelTableSliderFactorT = new Hashtable<Integer, Component>();
		labelTableSliderFactorT.put(0, new JLabel("0.0"));
		labelTableSliderFactorT.put(1000, new JLabel("1.0"));
		labelTableSliderFactorT.put(500, new JLabel("0.5"));
		sliderFatorT.setLabelTable(labelTableSliderFactorT);
		sliderFatorT.setPaintLabels(true);
		
		
		
        jScrollPane1 = new javax.swing.JScrollPane();
        listPontos = new javax.swing.JList();
        labelVelPolig = new javax.swing.JLabel();
        labelFatorT = new javax.swing.JLabel();
        checkBoxPoligonais = new javax.swing.JCheckBox();
        checkBoxCurva = new javax.swing.JCheckBox();
        checkBoxPontos = new javax.swing.JCheckBox();
        buttonRemoverPonto = new javax.swing.JButton();
        buttonAddCurve = new javax.swing.JButton();
        buttonPlayPause = new javax.swing.JButton();
        SpinnerModel spinnerEvaluationsModel = new SpinnerNumberModel(1000, 1, 90000, 5);
        spinnerEvaluations = new JSpinner(spinnerEvaluationsModel);
        spinnerEvaluations.setEditor(new JSpinner.NumberEditor(spinnerEvaluations));
        labelEvaluations = new JLabel();
        labelEvaluationsRange = new JLabel();
        labelTValue = new JLabel();
        checkBoxPoligonaisSecundarias = new JCheckBox();

        setMaximumSize(new java.awt.Dimension(211, 434));
        setMinimumSize(new java.awt.Dimension(211, 434));
        setLayout(null);
        
        comboBoxCurvas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboBoxCurvas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBoxCurvasActionPerformed(evt);
            }
        });
        add(comboBoxCurvas);
        comboBoxCurvas.setBounds(10, 34, 144, 20);

        labelCurva.setText("Curvas:");
        add(labelCurva);
        labelCurva.setBounds(10, 11, 50, 14);

        labelPontos.setText("Pontos:");
        add(labelPontos);
        labelPontos.setBounds(10, 61, 50, 14);
        add(sliderPolig);
        sliderPolig.setBounds(10, 210, 144, 50);
        add(sliderFatorT);
        sliderFatorT.setBounds(10, 280, 144, 50);

        listPontos.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listPontos);

        add(jScrollPane1);
        jScrollPane1.setBounds(10, 81, 144, 99);

        labelVelPolig.setText("Velocidade da poligonal:");
        add(labelVelPolig);
        labelVelPolig.setBounds(10, 190, 140, 14);


		labelFatorT.setText("Parametro T:");
		add(labelFatorT);
		labelFatorT.setBounds(10, 265, 120, 14);
		
		add(labelTValue);
		labelTValue
				.setText(((double) this.sliderFatorT.getValue() / this.sliderFatorT
						.getMaximum()) + "");
		labelTValue.setBounds(100, 265, 40, 14);

		checkBoxPontos.setText("Ativar pontos");
        checkBoxPontos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxPontosActionPerformed(evt);
            }
        });
        add(checkBoxPontos);
        checkBoxPontos.setBounds(10, 330, 150, 23);
		
        checkBoxPoligonais.setText("Ativar poligonais");
        checkBoxPoligonais.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxPoligonaisActionPerformed(evt);
            }
        });
        add(checkBoxPoligonais);
        checkBoxPoligonais.setBounds(10, 350, 130, 23);
        
        checkBoxPoligonaisSecundarias.setText("Ativar poligonais secundarias");
        checkBoxPoligonaisSecundarias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxPoligonaisSecundariasActionPerformed(evt);
            }
        });
        add(checkBoxPoligonaisSecundarias);
        checkBoxPoligonaisSecundarias.setBounds(10, 370, 170, 23);

        checkBoxCurva.setText("Ativar curva");
        checkBoxCurva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxCurvaActionPerformed(evt);
            }
        });
        add(checkBoxCurva);
        checkBoxCurva.setBounds(10, 390, 100, 23);

        buttonRemoverPonto.setText("Remover ponto");
        buttonRemoverPonto.setMargin(new java.awt.Insets(2, 2, 2, 2));
        buttonRemoverPonto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoverPontoActionPerformed(evt);
            }
        });
        add(buttonRemoverPonto);
        buttonRemoverPonto.setBounds(10, 420, 100, 30);

        buttonPlayPause.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/netbeans/play_pause16.png"))); // NOI18N
        buttonPlayPause.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPlayPauseActionPerformed(evt);
            }
        });
        add(buttonPlayPause);
        buttonPlayPause.setBounds(125, 420, 30, 30);
        
        //butaum criar curva
        buttonAddCurve.setText("Nova curva");
        buttonAddCurve.setMargin(new java.awt.Insets(2, 2, 2, 2));
        buttonAddCurve.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddCurveActionPerformed(evt);
            }
        });
        add(buttonAddCurve);
        buttonAddCurve.setBounds(10, 525, 100, 30);
        
        add(spinnerEvaluations);
        spinnerEvaluations.setBounds(80, 490, 60, 23);
        spinnerEvaluations.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent evt) {
				spinnerEvaluationsStateChanged(evt);

			}
		});

		add(labelEvaluations);
		labelEvaluations.setBounds(10, 460, 200, 20);
		labelEvaluations.setText("Quantidade de Avaliacoes:");

		add(labelEvaluationsRange);
		labelEvaluationsRange.setBounds(10, 490, 200, 20);
		labelEvaluationsRange.setText("(1 a 90000)");
		



	}// </editor-fold>//GEN-END:initComponents

	public void preencherListaCurvas() {

		int aux = this.target.getDraw().getCurveIndex();

		comboBoxCurvas.removeAllItems();

		for (int i = 0; i < this.target.getDraw().getCurves().size(); i++) {
			comboBoxCurvas.addItem("Curva" + i);
		}

		comboBoxCurvas.setSelectedIndex(aux);
		

    }
    
    public void preencherListaPontos() {
    	
    	listaDePontos.clear();
    	int aux = comboBoxCurvas.getSelectedIndex();
		
    	if(aux != -1) {

    		for (int i = 0; i < this.target.getDraw().getCurves().get(aux).getControlPoints().size(); i++) {
    			String str = "Ponto(" + (int) this.target.getDraw().getCurves().get(aux).getControlPoints().get(i).getX() +
    					" , " + (int) this.target.getDraw().getCurves().get(aux).getControlPoints().get(i).getY() + ")";
    			listaDePontos.addElement(str);
    		}
    	}
    	
    }

	private void comboBoxCurvasActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_comboBoxCurvasActionPerformed
		// TODO add your handling code here:
		preencherListaPontos();
		if(comboBoxCurvas.getSelectedIndex() != -1) {
    		this.target.getDraw().setCurveIndex(comboBoxCurvas.getSelectedIndex());
    	}
		Curva c = this.target.getDraw().getCurves()
				.get(this.target.getDraw().getCurveIndex());
		this.checkBoxCurva.setSelected(c.isCurveVisible());
		this.checkBoxPoligonais.setSelected(c.isPolygonalVisible());
		this.checkBoxPontos.setSelected(c.isControlPointsVisible());
		this.checkBoxPoligonaisSecundarias.setSelected(c.isSecondaryPolygonalsVisible());
		
	}// GEN-LAST:event_comboBoxCurvasActionPerformed

	private void checkBoxPoligonaisActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_checkBoxPoligonaisActionPerformed

		boolean visibility = checkBoxPoligonais.isSelected();
		Curva c = this.target.getDraw().getCurves()
				.get(this.target.getDraw().getCurveIndex());
		c.setPolygonalVisible(visibility);
		this.target.update();

	}// GEN-LAST:event_checkBoxPoligonaisActionPerformed

	private void checkBoxCurvaActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_checkBoxCurvaActionPerformed

		boolean visibility = checkBoxCurva.isSelected();
		Curva c = this.target.getDraw().getCurves()
				.get(this.target.getDraw().getCurveIndex());
		c.setCurveVisible(visibility);
		this.target.update();
    	
    }//GEN-LAST:event_checkBoxPontosActionPerformed
	
	private void checkBoxPontosActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_checkBoxPontosActionPerformed
		// TODO add your handling code here:

		boolean visibility = checkBoxPontos.isSelected();

		Curva c = this.target.getDraw().getCurves()
				.get(this.target.getDraw().getCurveIndex());
		c.setControlPointsVisible(visibility);
		this.target.update();
		/*
		 * >>>>>>>>>>>>>>>>>>>>> FALTA IMPLEMENTAR AQUI
		 * <<<<<<<<<<<<<<<<<<<<<<<<<<
		 */

	}// GEN-LAST:event_checkBoxPontosActionPerformed

    private void buttonRemoverPontoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoverPontoActionPerformed
        // TODO add your handling code here:
		Ponto aux = this.target.getMouse().getSelectedPoint();
		if (aux != null) {
			for (int i = 0; i < this.target.getDraw().getCurves().size(); i++) {
				Curva curveAux = this.target.getDraw().getCurves().get(i);
				for (int j = 0; j < curveAux.getControlPoints().size(); j++) {
					if (curveAux.getControlPoints().get(j).equals(aux)) {
						curveAux.getControlPoints().remove(j);

						j = curveAux.getControlPoints().size();
						i = this.target.getDraw().getCurves().size();

						curveAux.setModified(true);
					}
				}
			}
		}
		target.update();

	}// GEN-LAST:event_buttonRemoverPontoActionPerformed

	private void sliderPoligStateChanged(ChangeEvent evt) {

		JSlider source = (JSlider) evt.getSource();
		if (this.target.getDraw().isAnimatedPolygonals()) {
			this.target.setSpeed(Math.max(DrawPanel.MAX_SPACING_BETWEEN_FRAMES
					- source.getValue(), 10));
		}
    	//this.target.update(); //TODO is this needed?
    	
    }
	
	private void checkBoxPoligonaisSecundariasActionPerformed(java.awt.event.ActionEvent evt){
		boolean visibility = checkBoxPoligonaisSecundarias.isSelected();

		Curva c = this.target.getDraw().getCurves()
				.get(this.target.getDraw().getCurveIndex());
		c.setSecondaryPolygonalsVisible(visibility);
		this.target.update();
	}
    
    private void buttonPlayPauseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPlayPauseActionPerformed
        // TODO add your handling code here:
    	this.target.getDraw().setAnimatedPolygonals(!this.target.getDraw().isAnimatedPolygonals());
    	
    }//GEN-LAST:event_buttonPlayPauseActionPerformed
    
    private void buttonAddCurveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddCurveActionPerformed
		Curva c = new Curva(colors[this.target.getDraw().getNextColor()]);
		this.target.getDraw().incrementColor();
		if (this.target.getDraw().getNextColor() >= this.colors.length){
			this.target.getDraw().setNextColor(0);
		}
		
		c.setCurveVisible(this.target.isAllCurvesVisible());

		c.setPolygonalVisible(this.target.isAllPolygonalsVisible());
		c.setControlPointsVisible(this.target.isAllControlPointsVisible());
		c.setAnimated(this.target.getDraw().isAnimatedPolygonals());
		c.setSecondaryPolygonalsVisible(this.target.isAllSecondaryPolygonalsVisible());
		this.target.getDraw().addCurve(c);
		this.target.getDraw().setCurveIndex(this.target.getDraw().getCurves().size() - 1);
    }//GEN-LAST:event_buttonAddCurveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonPlayPause;
    private javax.swing.JButton buttonRemoverPonto;
    private javax.swing.JCheckBox checkBoxCurva;
    private javax.swing.JCheckBox checkBoxPoligonais;
    private javax.swing.JCheckBox checkBoxPontos;
    private javax.swing.JComboBox comboBoxCurvas;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCurva;
    private javax.swing.JLabel labelFatorT;
    private javax.swing.JLabel labelPontos;
    private javax.swing.JLabel labelVelPolig;
    private javax.swing.JList listPontos;
    private javax.swing.JSlider sliderFatorT;
    private javax.swing.JSlider sliderPolig;
    private javax.swing.JSpinner spinnerEvaluations;
    private javax.swing.JLabel labelEvaluations;
    private javax.swing.JLabel labelEvaluationsRange;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JButton buttonAddCurve;
	private javax.swing.JLabel labelTValue;
	private javax.swing.JCheckBox checkBoxPoligonaisSecundarias;


	private void spinnerEvaluationsStateChanged(ChangeEvent evt) {
		JSpinner source = (JSpinner) evt.getSource();
		for (int i = 0; i < this.target.getDraw().getCurves().size(); i++) {
			this.target.getDraw().getCurves().get(i)
					.setN_divisions((Integer) source.getValue());
		}
		this.sliderFatorT.setMaximum((Integer) source.getValue());
		
		Hashtable<Integer, Component> labelTableSliderFactorT = new Hashtable<Integer, Component>();
		labelTableSliderFactorT.put(0, new JLabel("0.0"));
		labelTableSliderFactorT.put((Integer) source.getValue(), new JLabel("1.0"));
		labelTableSliderFactorT.put((Integer)source.getValue()/2, new JLabel("0.5"));
		sliderFatorT.setLabelTable(labelTableSliderFactorT);
		sliderFatorT.setPaintLabels(true);
		
		this.target.update();

	}

	private void sliderFatorTStateChanged(ChangeEvent arg0) {
		JSlider source = (JSlider) arg0.getSource();
		boolean aux = this.target.getDraw().isAnimatedPolygonals();
		this.target.getDraw().setAnimatedPolygonals(false);
		for (int i = 0; i < this.target.getDraw().getCurves().size(); i++) {
			this.target
					.getDraw()
					.getCurves()
					.get(i)
					.settAnimation(
							(double) source.getValue()
									/ this.sliderFatorT.getMaximum());
		}
		this.labelTValue.setText((double) this.sliderFatorT.getValue()
				/ this.sliderFatorT.getMaximum() + "");
		this.target.update();
		this.target.getDraw().setAnimatedPolygonals(aux);

	}




	// End of variables declaration//GEN-END:variables
	public javax.swing.JCheckBox getCheckBoxCurva() {
		return checkBoxCurva;
	}

	public void setCheckBoxCurva(javax.swing.JCheckBox checkBoxCurva) {
		this.checkBoxCurva = checkBoxCurva;
	}

	public javax.swing.JCheckBox getCheckBoxPoligonais() {
		return checkBoxPoligonais;
	}

	public void setCheckBoxPoligonais(javax.swing.JCheckBox checkBoxPoligonais) {
		this.checkBoxPoligonais = checkBoxPoligonais;
	}

	public javax.swing.JCheckBox getCheckBoxPontos() {
		return checkBoxPontos;
	}

	public void setCheckBoxPontos(javax.swing.JCheckBox checkBoxPontos) {
		this.checkBoxPontos = checkBoxPontos;
	}

	public javax.swing.JSlider getSliderPolig() {
		return sliderPolig;
	}

	public void setSliderPolig(javax.swing.JSlider sliderPolig) {
		this.sliderPolig = sliderPolig;
	}

	public javax.swing.JSlider getSliderFatorT() {
		return sliderFatorT;
	}

	public void setSliderFatorT(javax.swing.JSlider sliderFatorT) {
		this.sliderFatorT = sliderFatorT;
	}

	public javax.swing.JCheckBox getCheckBoxPoligonaisSecundarias() {
		return checkBoxPoligonaisSecundarias;
	}

	public void setCheckBoxPoligonaisSecundarias(javax.swing.JCheckBox checkBoxPoligonaisSecundarias) {
		this.checkBoxPoligonaisSecundarias = checkBoxPoligonaisSecundarias;
	}

	
}
